<?php
declare (strict_types = 1);

namespace app\common\model\message;

use app\common\model\content\Article;
use app\common\model\user\User;
use think\Model;

/**
 * @mixin think\Model
 */
class Like extends Model
{
    // 设置字段信息
    protected $schema = [
        'id'            => 'int',
        'user_id'       => 'int',
        'article_id'    => 'int',
        'create_time'   => 'int',
    ];

    //类型转换
    protected $type = [
        'id'            => 'integer',
        'user_id'       => 'integer',
        'article_id'    => 'integer',
    ];

    //插入之前完善数据
    public static function onBeforeInsert(Model $model)
    {
        $model->setAttr('user_id',app()->user->getId());
    }

    //关联用户
    public function user()
    {
        return $this->belongsTo(User::class,'user_id', 'id')->bind([
            'username' => 'showName',
            'avatar' => 'avatarUrl'
        ]);
    }

    //关联文章
    public function article()
    {
        return $this->belongsTo(Article::class,'article_id', 'id');
    }
}
